package spring.cloud.tasks.executor.listener;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.executor.listener.analyse.AnalyseResetListenerHolder;
import spring.cloud.tasks.executor.listener.config.ConfigurationListenerHolder;
import spring.cloud.tasks.executor.listener.election.ElectionListenerHolder;
import spring.cloud.tasks.executor.listener.failover.FailoverListenerHolder;
import spring.cloud.tasks.executor.listener.report.ReportControlListenerHolder;
import spring.cloud.tasks.executor.listener.sharding.ShardingListenerHolder;
import spring.cloud.tasks.executor.listener.task_execute.TaskExecuteListenerHolder;
import spring.cloud.tasks.executor.task.TaskExecutor;

/**
 * 作业注册中心的监听器管理者.
 */
@Slf4j
@Setter
@Getter
public class ListenerHolderManager extends ListenerHolder {

    private ElectionListenerHolder electionListener;

    private FailoverListenerHolder failoverListener;

    private TaskExecuteListenerHolder taskExecuteListener;

    private ConfigurationListenerHolder configurationListener;

    private ShardingListenerHolder shardingListener;

    private AnalyseResetListenerHolder analyseResetListener;

    private ReportControlListenerHolder controlListener;

    public ListenerHolderManager(final TaskExecutor taskScheduler) {
        super(taskScheduler);
    }

    /**
     * 开启所有监听器.
     */
    @Override
    public void start() {
        electionListener = new ElectionListenerHolder(taskExecutor);
        failoverListener = new FailoverListenerHolder(taskExecutor);
        taskExecuteListener = new TaskExecuteListenerHolder(taskExecutor);
        configurationListener = new ConfigurationListenerHolder(taskExecutor);
        shardingListener = new ShardingListenerHolder(taskExecutor);
        analyseResetListener = new AnalyseResetListenerHolder(taskExecutor);
        controlListener = new ReportControlListenerHolder(taskExecutor);

        electionListener.start();
        failoverListener.start();
        taskExecuteListener.start();
        configurationListener.start();
        shardingListener.start();
        analyseResetListener.start();
        controlListener.start();
    }

    @Override
    public void stop() {
        electionListener.stop();
        failoverListener.stop();
        taskExecuteListener.stop();
        configurationListener.stop();
        shardingListener.stop();
        analyseResetListener.stop();
        controlListener.stop();
    }
}
